package com.kakaxi.mybilibili.module.home.advice.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.AdviceHomeItemBean;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.advice.model.AdviceHomeModel;
import com.kakaxi.mybilibili.module.home.advice.model.IAdviceHomeModel;
import com.kakaxi.mybilibili.module.home.advice.view.IAdviceHomeView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐主页面控制层实现类
 * Created by kakaxi on 16/11/20.
 */

public class AdviceHomePresenter implements IAdviceHomePresenter {

    /** 数据层 **/
    private IAdviceHomeModel mModel;

    /** 视图层 **/
    private IAdviceHomeView mView;

    public AdviceHomePresenter(IAdviceHomeView adviceHomeView) {
        mView = adviceHomeView;
        mModel = new AdviceHomeModel();
    }

    @Override
    public void initAdviceHomeData() {
        mView.showLoading();
        getAdviceHomeData();
    }

    @Override
    public void refreshAdviceHomeData() {
        getAdviceHomeData();
    }

    private void getAdviceHomeData() {
        mModel.getAdviceHomeListData(new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) {
                    String adviceHomeListArrStr = response.getJSONArray(WebParamConst
                            .RESPONSE_RESULT).toString();
                    final List<AdviceHomeItemBean> adviceHomeItemBeanList = JSON.parseArray
                            (adviceHomeListArrStr, AdviceHomeItemBean.class);

                    //--------------------------- 发送获取轮播数据请求 -------------------------------
                    mModel.getAdviceHomeBannerData(new Listener<JSONObject>() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            super.onSuccess(response);
                            mView.hideLoading();

                            if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                                    .RESULT_CODE_OK)) {
                                List<BannerItemBean> bannerItemBeanList = parseBannerJsonData
                                        (response.getJSONArray(WebParamConst.RESPONSE_DATA));
                                // 展示数据
                                mView.showAdviceHomeData(adviceHomeItemBeanList,
                                        bannerItemBeanList);
                            } else {
                                mView.hideLoading();
                                mView.showMessage(response.getString(WebParamConst
                                        .RESPONSE_MESSAGE));
                            }
                        }

                        @Override
                        public void onError(NetroidError error) {
                            super.onError(error);
                            mView.hideLoading();
                        }
                    });
                    //--------------------------- 发送获取轮播数据请求 -------------------------------


                } else {
                    mView.hideLoading();
                    mView.showMessage(response.getString(WebParamConst.RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }


    /**
     * 把轮播数据转化为通用轮播数据
     *
     * @param dataJsonArr 轮播json数组
     * @return 通用轮播数据数组
     */
    private List<BannerItemBean> parseBannerJsonData(JSONArray dataJsonArr) {
        List<BannerItemBean> bannerItemBeanList = new ArrayList<>();
        for (int i = 0; i < dataJsonArr.size(); i++) {
            JSONObject jsonObj = dataJsonArr.getJSONObject(i);
            BannerItemBean bannerItemBean = new BannerItemBean(
                    jsonObj.getString(WebParamConst.RESPONSE_DATA_ADVICE_HOME_BANNER_TITLE),
                    jsonObj.getString(WebParamConst.RESPONSE_DATA_ADVICE_HOME_BANNER_IMAGE),
                    jsonObj.getString(WebParamConst.RESPONSE_DATA_ADVICE_HOME_BANNER_REMARK),
                    jsonObj.getString(WebParamConst.RESPONSE_DATA_ADVICE_HOME_BANNER_VALUE)
            );
            bannerItemBeanList.add(bannerItemBean);
        }
        return bannerItemBeanList;
    }

}
